package com.meilai.project.controller.app.appController;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.app.VerInfoDTO;
import com.meilai.project.service.app.AppExtraService;
import com.meilai.project.service.middleware.AttachmentService;
import com.meilai.project.vo.app.CompanyOfflineDataVO;
import com.meilai.project.vo.app.UploadBackVO;
import com.meilai.project.vo.app.VerInfoVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张驰
 * @date 2021/10/30 21:55
 */
@Api(value = "app额外控制器", tags = {"app额外"})
@RestController
@RequestMapping("api/app/extra")
public class AppExtraController {

    @Autowired
    private AttachmentService attachmentService;
    @Autowired
    private AppExtraService appExtraService;


    @ApiOperation(value = "获取公司离线数据包", notes = "获取公司离线数据包", response = CompanyOfflineDataVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("get-offline-data")
    public CommonResult getOfflineData() {
        return new CommonResult().success(appExtraService.getCompanyOfflineData());
    }

    @ApiOperation(value = "上传多个照片", notes = "上传多个照片，swagger测试会出错，需要postman进行测试", response = UploadBackVO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files",value = "多个照片",paramType = "form", allowMultiple=true, required = true, dataType = "__file")
    })
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping(value = "upload-img-list", headers = "Content-Type=multipart/form-data")
    public CommonResult uploadList(@RequestParam(value = "files", required = true) MultipartFile[] files) throws IOException {
        if(files==null || files.length==0) return new CommonResult().failed("请传入files");
        List<UploadBackVO> vos = new ArrayList<>();
        for(MultipartFile file: files){
            String originalFilename = file.getOriginalFilename();
            UploadBackVO vo = attachmentService.storeBackVO(file.getInputStream(), file.getSize(), file.getContentType(), originalFilename);
            vos.add(vo);
        }
        return new CommonResult().success(vos);
    }

    @ApiOperation(value = "获取我的基本信息", notes = "获取我的基本信息")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("get-my-info")
    public CommonResult getMyInfo(HttpServletRequest request) {
        return new CommonResult().success(appExtraService.getMyInfo(request));
    }

    @ApiOperation(value = "获取版本信息", notes = "获取版本信息", response = VerInfoVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("get-ver-info")
    public CommonResult getVerInfo(@RequestBody @ApiParam(name = "当前版本信息入参对象", value = "传入json格式") VerInfoDTO dto) {
        return new CommonResult().success(appExtraService.getVerInfo(dto));
    }
}
